package com.haiyou.dc.es.controller;

import com.github.pagehelper.PageInfo;
import com.haiyou.common.util.R;
import com.haiyou.dc.es.bi.impl.EsBookNovelService;
import com.haiyou.dc.es.entity.NovelEntity;
import com.haiyou.dc.es.entity.form.SearchNovelForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author pc
 */
@RequestMapping("/es")
@RestController
@Slf4j
@Api(tags = "ES操作")
public class EsController {

    @Autowired
    private EsBookNovelService esBookNovelService;

    @ApiOperation(value = "获取数据")
    @GetMapping("book/novel/get/{id}")
    public R getBookNovel(@PathVariable(value = "id") String id) {
        Object result = esBookNovelService.getById(id);
        return R.ok().put("data", result);
    }

    @ApiOperation(value = "添加数据")
    @PostMapping("/book/novel/add")
    public R addBookNovel(@RequestBody NovelEntity novel) {
        log.info("----------------------->接收参数={}", novel);
        Boolean result = esBookNovelService.create(novel);
        if (!result) {
            return R.error("添加失败");
        }
        return R.ok();
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping("book/novel/delete/{id}")
    public R deleteBookNovel(@PathVariable(value = "id") String id) {
        Boolean result = esBookNovelService.remove(id);
        if (!result) {
            return R.error("删除失败");
        }
        return R.ok();
    }

    @ApiOperation(value = "更新数据")
    @PutMapping("/book/novel/update")
    public R updateBookNovel(@RequestBody NovelEntity novel) {
        log.info("----------------------->接收参数={}", novel);
        if (StringUtils.isEmpty(novel.getId())) {
            return R.error("ID参数不能为空");
        }
        Boolean result = esBookNovelService.update(novel.getId(), novel);
        if (!result) {
            return R.error("更新失败");
        }
        return R.ok();
    }

    @ApiOperation(value = "搜索数据")
    @PostMapping("/book/novel/search")
    public R addBookNovel(@RequestBody SearchNovelForm form) {
        log.info("----------------------->接收参数={}", form);
        PageInfo pageInfo = esBookNovelService.searchBookNovel(form);
        return R.ok(pageInfo);
    }
}
